import VueRouter from "vue-router";
import Vue from "vue";

// 用import函数引入组件 达到懒加载的效果
const Home = () => import('@/pages/Home')
const Login = () => import('@/pages/Login')
const NotFound = () => import('@/pages/NotFound/404')
const Game = () => import('@/pages/Home/Game')
const GameDetail = () => import('@/pages/Home/Game/GameDetail')
const Music = () => import('@/pages/Home/Music')
const MusicDetail = () => import('@/pages/Home/Music/MusicDetail')
const News = () => import('@/pages/Home/News')
const NewsDetail = () => import('@/pages/Home/News/NewsDetail')


Vue.use(VueRouter)

const router = new VueRouter({
  mode:'history',
  // 配置路由表
  routes:[
    {
      path:'/',
      redirect:"/home" // 重定向 默认去首页
    },
    {
      path:'/home',
      component:Home,
      // name:"Home"  如果一个路由拥有了默认子路由,请不要设置name命名路由
      children:[
        {
          path:'news',
          component:News,
          name:"news",
          children:[
            {
              path: "newsdetail",
              component: NewsDetail,
              name: "newsdetail",

              
              // props传参
              props: (route) => {
                //如果props是一个函数,则可以在内部接受到当前路由组件的$route对象
                console.log(route, "route");
                return {
                  ...route.params,
                  ...route.query,
                  classRoom: "0516",
                  count: "71",
                };
              },


            }
          ]
        },
        {
          path:'game',
          component:Game,
          name:"game",
          children:[
            {
              path: "gamedetail",
              component: GameDetail,
              name: "gamedetail",
              // 路由元信息  将附加信息加到路由上 接收方法: this.$route.meta 或 props
              meta: {
                hidden: true,
                icon: "plus",
              },
            }
          ]
        },

        {
          path:'music',
          component:Music,
          name:"music",
          children:[
            {
              // params参数  age设置为可选
              path: "musicdetail/:id/:age",
              component: MusicDetail,
              name: "musicdetail",
            }
          ]
        },


      ]
    },
    {
      path:'/login',
      component:Login,
      name:"Login"
    },


    {
      path:'*',
      component:NotFound,
    }
  ]
})


//  判断是否登录 
const isLogin = false

router.beforeEach((to, from, next) => {
  // console.log(to, from, next, "全局前置守卫");
  console.log(to.path); // 当前路径

  /* 
  校验登录
  if(to.path === '/home' && isLogin === false) {
    alert('请登录')
    next(false) // 组织跳转
  }else{
    next();
  } */

  next()
 
  // next("/login");
});
router.beforeResolve((to, from, next) => {
  // console.log(to, from, next, "全局解析守卫");
  next();
});





export default router